package com.it.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.it.model.Book;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @desc:
 * @author: panxiyi
 * @date: 2023/4/12 12:48 下午
 */

@SpringBootTest
public class BookServiceTestCase {

    @Autowired
    private IBookService bookService;

    @Test
    public void getByIdTest() {
        Book book = bookService.getById(1L);
        System.out.println(book);
    }


    @Test
    public void saveTest() {
        Book book = new Book(7L, "音乐", "二胡---", "好听的很");
        bookService.save(book);
    }

    @Test
    public void getByPage() {
        //IPage<Book> page = bookService.getByPage(3, 2);
        IPage<Book> page = new Page<Book>(3, 2);
        bookService.page(page);
        page.getRecords().forEach(System.out::println);
    }

    @Test
    public void updateTest() {
        Book book = new Book();
        book.setId(6L);
        book.setDescription("羌笛何须怨杨柳中的笛子");
        bookService.updateById(book);
    }

    @Test
    public void getALl() {
        System.out.println(bookService.list());
    }

    @Test
    public void parseArray() {
        //List<Book> books = JSON.parseArray("", Book.class);
        //System.out.println("books: " + books);
    }

    @Test
    public void subStr() {
        String[] materialIds = StringUtils.split("aa,bb", ",");
        //System.out.println(materialIds.con);
    }

    @Test
    public void testInteger() {
        Integer a = null;
        int i = 9;
        if (a == i) {
            System.out.println("o");
        }
    }

    @Test
    public void JSONparse() {
        String s = "[{\"id\":\"1000001\",\"product_id\":\"com.meitu.mtxx.consume.ai_editing.1000001\",\"platform\":\"ios\",\"rights_id\":\"91100001\",\"subject\":\"AI改图单次体验\",\"priceInfo\":[{\"price\":200,\"originalPrice\":200,\"count\":1,\"countryCode\":\"CN\"},{\"price\":99,\"originalPrice\":99,\"count\":1,\"countryCode\":\"US\"},{\"price\":16000,\"originalPrice\":16000,\"count\":1,\"countryCode\":\"JP\"},{\"price\":199,\"originalPrice\":199,\"count\":1,\"countryCode\":\"CA\"},{\"price\":3000,\"originalPrice\":3000,\"count\":1,\"countryCode\":\"TW\"},{\"price\":150000,\"originalPrice\":150000,\"count\":1,\"countryCode\":\"KR\"},{\"price\":800,\"originalPrice\":800,\"count\":1,\"countryCode\":\"HK\"},{\"price\":3500,\"originalPrice\":3500,\"count\":1,\"countryCode\":\"TH\"},{\"price\":2500000,\"originalPrice\":2500000,\"count\":1,\"countryCode\":\"VN\"},{\"price\":148,\"originalPrice\":148,\"count\":1,\"countryCode\":\"SG\"},{\"price\":149,\"originalPrice\":149,\"count\":1,\"countryCode\":\"AU\"},{\"price\":99,\"originalPrice\":99,\"count\":1,\"countryCode\":\"GB\"},{\"price\":490,\"originalPrice\":490,\"count\":1,\"countryCode\":\"RY\"},{\"price\":1600000,\"originalPrice\":1600000,\"count\":1,\"countryCode\":\"ID\"},{\"price\":490,\"originalPrice\":490,\"count\":1,\"countryCode\":\"BR\"},{\"price\":99,\"originalPrice\":99,\"count\":1,\"countryCode\":\"MO\"},{\"price\":4900,\"originalPrice\":4900,\"count\":1,\"countryCode\":\"PH\"},{\"price\":299,\"originalPrice\":299,\"count\":1,\"countryCode\":\"NZ\"}]},{\"id\":\"1000001\",\"product_id\":\"com.meitu.mtxx.onetime.ai_cartoon.1000002\",\"platform\":\"google\",\"rights_id\":\"91100001\",\"subject\":\"AI改图单次体验\",\"priceInfo\":[{\"price\":200,\"originalPrice\":200,\"count\":1,\"countryCode\":\"CN\"},{\"price\":99,\"originalPrice\":99,\"count\":1,\"countryCode\":\"US\"},{\"price\":16000,\"originalPrice\":16000,\"count\":1,\"countryCode\":\"JP\"},{\"price\":199,\"originalPrice\":199,\"count\":1,\"countryCode\":\"CA\"},{\"price\":3000,\"originalPrice\":3000,\"count\":1,\"countryCode\":\"TW\"},{\"price\":150000,\"originalPrice\":150000,\"count\":1,\"countryCode\":\"KR\"},{\"price\":800,\"originalPrice\":800,\"count\":1,\"countryCode\":\"HK\"},{\"price\":3500,\"originalPrice\":3500,\"count\":1,\"countryCode\":\"TH\"},{\"price\":2500000,\"originalPrice\":2500000,\"count\":1,\"countryCode\":\"VN\"},{\"price\":148,\"originalPrice\":148,\"count\":1,\"countryCode\":\"SG\"},{\"price\":149,\"originalPrice\":149,\"count\":1,\"countryCode\":\"AU\"},{\"price\":99,\"originalPrice\":99,\"count\":1,\"countryCode\":\"GB\"},{\"price\":490,\"originalPrice\":490,\"count\":1,\"countryCode\":\"RY\"},{\"price\":1600000,\"originalPrice\":1600000,\"count\":1,\"countryCode\":\"ID\"},{\"price\":490,\"originalPrice\":490,\"count\":1,\"countryCode\":\"BR\"},{\"price\":99,\"originalPrice\":99,\"count\":1,\"countryCode\":\"MO\"},{\"price\":4900,\"originalPrice\":4900,\"count\":1,\"countryCode\":\"PH\"},{\"price\":299,\"originalPrice\":299,\"count\":1,\"countryCode\":\"NZ\"}]},{\"product_id\":\"1000001\",\"id\":\"1000001\",\"platform\":\"android\",\"rights_id\":\"91100001\",\"subject\":\"AI改图单次体验\",\"priceInfo\":[{\"price\":200,\"originalPrice\":200,\"count\":1,\"countryCode\":\"CN\"},{\"price\":99,\"originalPrice\":99,\"count\":1,\"countryCode\":\"US\"}]}]";
        Map<String, Integer> map = new HashMap<>();
        JSONArray jsonArray = JSON.parseArray(JSON.parseObject(JSON.parseArray(s).get(0).toString()).getString("priceInfo"));
        List<String> countrys = Arrays.asList("cn","jp", "tw", "us", "hk", "th", "vn", "my", "kr", "sg", "ca", "id", "au", "mo");
        List<String> rightsIds = Arrays.asList("91100001", "91100002");
        Map<String, Object> mapCountrys = new HashMap<>();
        for (String country : countrys) {
            Map<String, Object> mapRights = new HashMap<>();
            for (String rightsId : rightsIds) {
                Map<String, Integer> mapnum = new HashMap<>();
                if ("CN".equalsIgnoreCase(country)) {
                    if (rightsId.equals("91100001")) {
                        mapnum.put("vip_num", 30);
                        mapnum.put("not_vip_num", 5);
                    } else if (rightsId.equals("91100002")) {
                        mapnum.put("vip_num", 30);
                        mapnum.put("not_vip_num", 3);
                    }

                } else {
                    if (rightsId.equals("91100001")) {
                        mapnum.put("vip_num", 30);
                        mapnum.put("not_vip_num", 5);
                    } else if (rightsId.equals("91100002")) {
                        mapnum.put("vip_num", 30);
                        mapnum.put("not_vip_num", 3);
                    }
                }
                mapRights.put(rightsId, mapnum);
            }
            mapCountrys.put(country.toLowerCase(), mapRights);
        }
        System.out.println(JSON.toJSONString(mapCountrys));
        String configStr = JSON.toJSONString(mapCountrys);
        JSONObject countryMapCounts = JSON.parseObject(configStr);
        String rightsIdMapCountsConfigStr = countryMapCounts.getString("cn");

        JSONObject rightsIdMapCounts = JSON.parseObject(rightsIdMapCountsConfigStr);
        String rightsMapNumsConfigStr = rightsIdMapCounts.getString("91100002");

        JSONObject mapNumObject = JSON.parseObject(rightsMapNumsConfigStr);
        Integer vipRightsNum = mapNumObject.getInteger("vip_num");
        Integer notVipRightsNum = mapNumObject.getInteger("not_vip_num");
        System.out.println(vipRightsNum + " =========== > " + notVipRightsNum);
    }

}
